package com.cplatform.movie.back.sale.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.cplatform.movie.back.sale.dao.ImageDetailsDao;
import com.cplatform.movie.back.sale.entity.ImageDetails;
import com.cplatform.movie.back.sale.entity.ItemSale;
/**
 * Title. 商品图片业务层
 * <br>
 * Description.
 * <p>
 * Author: liuyt
 * <p>
 * Version: 1.0
 * <p>
 */

@Service
public class ImageDetailService {
	@Autowired
	private ImageDetailsDao imageDetailsDao;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * @Title: 批量新增商品详情图片 
	 * <br>
	 * @param itemSale		商品
	 * @param jzImgArray	图片path数组
	 * @throws Exception    
	 * @throws
	 */
	public void saveImageDetails(ItemSale itemSale, String[] jzImgArray, String[] jzImgOrderArray, String[] jzImgIdArray) throws Exception {
		Long id = itemSale.getId();
		List<ImageDetails> list = new ArrayList<ImageDetails>();
		ImageDetails details = null;
		for (int i = 0; i < jzImgArray.length; i++) {
			details = new ImageDetails();
			// -- 如果为图片更新， 只修改排序值
			if (StringUtils.isNotBlank(jzImgIdArray[i])) {
				details.setId(Long.parseLong(jzImgIdArray[i]));
				details.setOrderNum(Long.parseLong(jzImgOrderArray[i])); // 图片排序值
			} else {
				details.setImageType("1");
				details.setImagePath(jzImgArray[i]);
				details.setItemId(id);
				details.setOrderNum(Long.parseLong(jzImgOrderArray[i])); // 图片排序值
			}
			list.add(details);
		}
		imageDetailsDao.save(list);
	}
	
	/** 根据id删除单条详情图片**/
	public void deleteById(Long id) {
		imageDetailsDao.delete(id);
	}

	/**
	 * @Title: 根据商品ID 查询商品详情图片  key:图片地址    value：图片排序数值 
	 * <br>
	 * @param id
	 * @return    
	 * @throws
	 */
	public List<Map<String, Object>> findImageInfoByItemId(Long id) {
		String sql = "select t.IMAGE_PATH, t.ORDER_NUM, t.ID from t_small_image_details t where t.item_id=? order by t.ORDER_NUM desc";
		return jdbcTemplate.queryForList(sql,new Object[]{id});
	}

}
